package com.ddm.service.impl;

import com.ddm.service.IStorage;
import com.entity.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import java.io.InputStream;
import java.util.List;

/**
 * Created by he on 2025/5/18 16:45
 */
@Service
public class FileService {
    private final String provider = "aliyun";
    @Value("${oss.aliyun.bucketName}")
    private String bucketName;
    @Autowired
    @Qualifier("aliyunStorage")
    private IStorage storage;
    /**
     * 上传文件
     * @param file
     * @return
     */
    public FileInfo uploadFile(MultipartFile file) {
        FileInfo fileInfo = getFileInfo(file);
        try {
            storage.putObject(fileInfo.getBucketName(), fileInfo.getFileName(), file.getInputStream(), fileInfo.getContentType());
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 得到上传文件的信息
     * @param file
     * @return
     */
    private FileInfo getFileInfo(MultipartFile file){
        final String fileOriginalName = file.getOriginalFilename();
        String objectName = IdUtil.fastSimpleUUID();
        String fileName = objectName;
        String fileSuffix = fileOriginalName.substring(fileOriginalName.lastIndexOf(".") + 1);
        if (StrUtil.isNotEmpty(fileSuffix)) {
            fileName = objectName.concat(String.valueOf(CharUtil.DOT)).concat(fileSuffix);
        }
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileName(fileName);
        fileInfo.setFileSize(file.getSize());
        fileInfo.setContentType(file.getContentType());
        fileInfo.setStorage(provider);
        fileInfo.setBucketName(bucketName);
        return fileInfo;
    }

    /**
     * 根据名称得到文件对象流
     * @param fileName
     * @return
     */
    public InputStream getFileStream(String fileName) {
        try {
            return storage.getObject(bucketName, fileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据名称删除文件
     * @param fileName
     */
    public void removeFileByName(String fileName) {
        try {
            storage.removeObject(bucketName,fileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据名称得到公共访问文件url
     * @param fileName
     * @return
     */
    public String getPublicFileUrl(String fileName) {
        try {
            return storage.getPublicObjectUrl(bucketName, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据名称得到私有文件签名url
     * @param objectName
     * @return Url含过期时间和签名信息
     */
    public String getPrivateFileUrl(String objectName) {
        try {
            return storage.getSignedObjectUrl(bucketName, objectName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 列出所有文件名称
     * @return
     */
    public List<String> listAllFileNames(){
        try {
            return storage.listAllObjectNames(bucketName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
